<?php
/**
 * games.php
 * $Id: games.php 21 2009-09-03 08:10:09Z ahmtsari $
 */

include_once("includes/evolt/database.php");
include_once("game.php");
include_once("actions.php");
include_once("team.php");
include_once("prediction.php");
include_once("league.php");
include_once("ui/init.php");

class Games extends Actions {
	var $games = array();
	var $error;
	
	function action($post_vars) {
		global $session;
		
		$return_str = "";
		if ($session->isAdmin()) { 
			switch ($post_vars['action']) {		
				default:
				case "Form":
					$return_str .= $this->form ($post_vars);
					$return_str .= $this->view ($post_vars);
					break;		
								
				case "Add":
				case "Update":
					$this->update($post_vars);
					$return_str .= $this->form ($post_vars);
					$return_str .= $this->view ($post_vars);
					break;
				case "Delete":
					$this->delete($post_vars);
					$return_str .= $this->form ();
					$return_str .= $this->view ($post_vars);
					break;
				
				case "pUpsert":
					$this->pUpserts($post_vars);
				case "pView":
					$return_str .= $this->pView ($post_vars);
					break;
					
				case "resultUpdate":
					$this->rUpdate($post_vars);
				case "resultView":
					$return_str .= $this->rView ($post_vars);
					break;				
	
				case "calculateAll":
				case "calculate":
					$this->calculate($post_vars);
				case "userTable":
					$return_str .= $this->userTable($post_vars);
					break;					
					
				case "Soon":
					$return_str .= $this->soon ($post_vars);
					$return_str .= $this->view ($post_vars);
					break;	

				case "reCalc":
					$return_str .= $this->reCalc($post_vars);
					$return_str .= $this->userPredictions($post_vars, $post_vars['username']);
					break;			
							
				case "userPredictions":
					$return_str .= $this->userPredictions($post_vars, $post_vars['userName']);
					break;	
			}
		}
		else if ($session->logged_in) {
			switch ($post_vars['action']) {		
				default:
				case "pView":
					$return_str .= $this->pView ($post_vars);
					break;
					
				case "pUpsert":
					$this->pUpserts($post_vars);
					$return_str .= $this->pView ($post_vars);
					
				case "userTable":
					$return_str .= $this->userTable($post_vars);
					break;

				case "userPredictions":
					$return_str .= $this->userPredictions($post_vars, $post_vars['userName']);
					break;											
			}			
		}
		else
			$return_str .= $this->userTable($post_vars);
			
		return $return_str;
	}	
	
	function view($post_vars) {
		global $database;  //The database connection
		if (isset($post_vars))
			extract($post_vars);
		
		$game = new Game();
		if (isset($leagueId))
			$gameList = $game->getList($leagueId);
		else
			$gameList = $game->getList();
			
		require_once("ui/init.php");
		$smarty = new _Smarty();
		foreach ($gameList as $row) {
			$g = new Game($row["id"]);
			$smarty->append('games', $g);
		}				
		
		$smarty->assign ($post_vars);
		return $smarty->fetch ('gameList.html');		
	}

	//------------------------------------------------
	function form ($post_vars = NULL) {
		global $database, $session;
		if (isset($post_vars))
			extract($post_vars);
			
		include_once("ui/init.php");
		$smarty = new _Smarty();
		$smarty->setDefaults();
		$smarty->assign ($post_vars);
		
		$game_date = date('Y-m-d H:i:s', $session->gameTime);
		if (isset($id)) {
			$game = new Game($id);
			$smarty->assign ("game", $game);
			$game_date = $game->date;
		} elseif (isset($Time_Hour))
			$game_date = $date . " " . $Time_Hour . ":" . $Time_Minute;
		$smarty->assign ("game_date", $game_date);
		
		$t = new Team(0,"","");
		foreach ($t->getList() as $row) {
			$t2 = new Team($row["Id"], $row["Team"], $row["Memo"]);
			$smarty->append("teams", $t2);
			unset($t2);
		}
		foreach ($database->getLeagues() as $row) {
			$league = new League($row["id"], $row["league"], $row["season"]);
			$smarty->append('leagues', $league);
			unset($league);
		}	

		if ($this->error != NULL)
			$smarty->assign ("error", $this->error);
		return $smarty->fetch ('gamesForm.html');		
	}
	
	//------------------------------------------------
	function update($post_vars) {
		if (isset($post_vars))
			extract($post_vars);

		if (!isset($leagueId) && $action=="Add") {
			$this->error = "Önce lig secmelisiniz!";
			return;
		}
		$game = new Game();
		$game->upsert($id, $date . " " . $Time_Hour . ":" . $Time_Minute, $home_team_id, $away_team_id, $day, $leagueId);			
	}
	//------------------------------------------------
	function delete($post_vars) {
		if (isset($post_vars))
			extract($post_vars);

		if (isset($id)) {
			$game = new Game($id);
			$game->delete();
		}			
	}	
	//------------------------------------------------
	function pView($post_vars) {
		global $session;
		
		$this->setGames($post_vars, 0);
		if (isset($post_vars))
			extract($post_vars);

		foreach ($this->games as $game)	{
			$p = new Prediction();
			$p->setPrediction($game->id);
			$game->prediction = $p;
			unset($p);
		}
		
		$smarty = new _Smarty();
		$smarty->assign ($post_vars);
		$smarty->assign ("games", $this->games);	
		$smarty->assign ("my_session", $session);
		return $smarty->fetch ('predList.html');	
	}
	//------------------------------------------------
	function pUpserts($post_vars) {
		if (isset($post_vars))
			extract($post_vars);

		$prediction = new Prediction($id);
		$prediction->home_score = $home_score;
		$prediction->away_score = $away_score;
		$prediction->upsert($game_id);			
	}	
	//------------------------------------------------
	function setGames($post_vars, $type=0) {
		unset($this->games);
		$this->games = array();
		if (isset($post_vars))
			extract($post_vars);

		$game = new Game();
//		if (isset($leagueId))
		switch ($type) {		
			case -1:
				$gameList = $game->getListOpen($leagueId);
				break;
			default:
			case 0:
				$gameList = $game->getList($leagueId);
				break;
			case 1:
				$gameList = $game->getListComplete($leagueId);
				break;
			case 2:
				$gameList = $game->getListCompleted($leagueId);
				break;
		}
				
//		else
//			$gameList = $game->getList();
					
		//assign prediction if exists
		foreach ($gameList as $row)	{
			$g = new Game($row["id"]);
			array_push($this->games, $g);
			unset($g);
		}
	}
	//------------------------------------------------
	function rView($post_vars) {
		global $database, $session;
		$database->query("UPDATE games SET home_team_score = NULL, away_team_score = NULL WHERE game_date > '$session->gameDBTime';");
			
		$this->setGames($post_vars, 1);
		if (isset($post_vars))
			extract($post_vars);

		foreach ($this->games as $game)	{
			$p = new Prediction();
			$p->setPrediction($game->id);
			$game->prediction = $p;
			unset($p);
		}
		
		include_once("ui/init.php");
		$smarty = new _Smarty();
		$smarty->assign ($post_vars);
		$smarty->assign ("games", $this->games);	
		return $smarty->fetch ('resultList.html');	
	}
	
	//------------------------------------------------
	function rUpdate($post_vars) {
		if (isset($post_vars))
			extract($post_vars);

		if (isset($id)) {
			$game = new Game($id);
			$game->home_score = $home_team_score;
			$game->away_score = $away_team_score;
			$game->resultUpdate();
		}			
		$this->calculate($post_vars);
	}	
	//------------------------------------------------
	function calculate($post_vars) {
		global $database;
				
		if (isset($post_vars))
			extract($post_vars);

		//get userliste
		if (isset($id)) {
			$game = new Game($id);
			$p = new Prediction();
			foreach ($p->userList($id) as $user)	{
				$game->calculateUser($user->username);
			}
		}	
		else {
			$database->query("UPDATE predictions SET pts = 0");	
			$this->setGames($post_vars, 2);
			foreach ($this->games as $game)	{
				$p = new Prediction();
				foreach ($p->userList($game->id) as $user)	{
					$game->calculateUser($user->username);
				}
				unset($p);				
			}
		}
	}	
	//------------------------------------------------
	function userTable($post_vars) {
		global $session;
		
		if (isset($post_vars))
			extract($post_vars);

		include_once("ui/init.php");
		$smarty = new _Smarty();
		$smarty->assign ($post_vars);
		$smarty->assign('my_session', $session);
		
		$p = new Prediction();
		$smarty->assign("users", $p->userList());
		
		return $smarty->fetch ('userList.html');
	}		
	//---------------------------------------------------
	function userPredictions($post_vars, $userName ="") {
		global $session;
		
		include_once("ui/init.php");
		$smarty = new _Smarty();
		$smarty->assign ($post_vars);
		$smarty->assign('my_session', $session);
		
		if (isset($post_vars))
			extract($post_vars);		
		
		$p = new Prediction();
		if (isset($game_id)) 
			$smarty->assign("games", $p->getList("", $game_id));
		else
			$smarty->assign("games", $p->getList($userName));
		
		return $smarty->fetch ('predGameList.html');
	}
	
	//------------------------------------------------
	function reCalc($post_vars) {
		global $database;
				
		if (isset($post_vars))
			extract($post_vars);

		//get userliste
		if (isset($id) && isset($username)) {
			$game = new Game($id);
			$game->calculateUser($username);
		}
		return "";	
	}		
}

?>
